/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;

import static org.sss.mapreduce.datatype.MsgPack.instance;

import java.io.IOException;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.msgpack.unpacker.BufferUnpacker;
import org.sss.mapreduce.Decoder;
import org.sss.mapreduce.SssException;

/**
 * A Packable class that encapsulates a JSONObject.
 */
// TODO: $B$I$&$7$h!#$3$l(B?
public class PackableJSONObject implements SelfPackable {
  private static final String NULL_JSON = "null";
  private String json;
  /**
   * Constructs a newly allocated PackableJSONObject object that it represents
   * null.
   */
  public PackableJSONObject() { this(NULL_JSON); }
  /**
   * Constructs a newly allocated PackableJSONObject object that represents
   * the primitive String argument.
   * @param original the value to be represented by the PackableJSONObject.
   */
  public PackableJSONObject(String original) {
    if (original == null) { original = NULL_JSON; }
    this.json = original;
  }
  /**
   * Return the JSONObject value of this PackableJSONObject object.
   * @return the JSONObject value represented by this object
   */
  public JSONObject get() {
    return (JSONObject) JSONValue.parse(this.json);
  }
  /**
   * Return the JSON value of this PackableJSONObject object.
   * @return the JSON value represented by this object
   */
  public String getJSON() {
    return this.json;
  }
  @Override
  public boolean equals(Object o) {
    if (o instanceof PackableJSONObject) {
      if (o == this) {
        return true;
      }
      return json.equals(((PackableJSONObject) o).getJSON());
    }
    else {
      return false;
    }
  }
  /**
   * Returns a hash code value for the object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return json.hashCode();
  }
  /**
   * Indicates which slot this object should be put.
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return hashCode() & 0x0f;
  }
  /**
   * Deserialize this object from the specified raw byte array.
   * see {@link SelfPackable#loadBytes}
   */
  @Override
  public PackableJSONObject loadBytes(byte[] bytes) throws Exception {
    this.json = instance.read(bytes, String.class);
    return this;
  }
  /**
   * Serialize this object into a sequence of bytes.
   * see {@link SelfPackable#toBytes}
   */
  @Override
  public byte[] toBytes() throws Exception {
    return instance.write(this.json);
  }

  @Override
  public long getRoughSize() {
    return 48 + json.length() * 2;
  }

  public static Decoder createDecoder() throws SssException {
    return new Decoder() {
      private BufferUnpacker unpacker = instance.createBufferUnpacker();
      @Override
      public Packable decode(byte[] b) throws IOException {
        unpacker.wrap(b);
        return new PackableJSONObject(unpacker.readString());
      }
    };
  }
}
